package u1;

import static java.lang.System.*;
import static u1.Util.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.regex.Matcher;

/**
 * Like NumberTitles but produces a map between Netflix movie IDs and Netflix
 * titles. Takes the netflixMovies.txt file on standard in. Takes a the name of
 * a file containing a list of netflix movie ids (whitespace delimited) as
 * its only argument. The format of the netflixMovies.txt file is:
 * movie_id year title
 * ...
 * 
 * The output format is title (year), one per line. The title input and output
 * is assumed to be UTF-8. The ordering of the movie IDs file is preserved in
 * the output.
 */
public class MovieIdTitles {
  public static void main(String [] args) throws IOException {
    if (args.length != 1) {
      err.println("See comments for usage.");
      exit(-1);
    }
    File targetFile = new File(args[0]);
    
    /* 
     * Read the titles into a map; there aren't that many. This also allows
     * us to easily preserve the ordering of the movie IDs file.
     */
    trace("Reading titles...");
    Map<Integer, String> titles = new HashMap<Integer, String>();
    BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-8"));
    String line = br.readLine();
    while (line != null) {
      Matcher m = NETFLIX_MOVIES_RECORD.matcher(line);
      if (!m.matches()) {
        trace("Failed to parse %s.", line);
      } else {
        Integer movieId = Integer.valueOf(m.group(1));
        Integer year = Integer.valueOf(m.group(2));
        String title = m.group(3);
        titles.put(movieId, String.format("%s (%d)", title, year));
      }
      line = br.readLine();
    }
    trace("Read %d Netflix titles.", titles.size());
    
    /*
     * Read movie target ids and output titles as we go.
     */
    Scanner s = new Scanner(targetFile);
    while (s.hasNextInt()) {
      int movieId = s.nextInt();
      String title = titles.get(movieId);
      if (title == null) {
        trace("Failed to find title for id %d.", movieId);
      } else {
        out.println(title);
      }
    }
  }
}
/*
* Copyright (c) 2009 John Lees-Miller
* 
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
* 
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/

